package me.bcloud.web.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.bcloud.web.dao.UserDao;
import me.bcloud.web.domain.JobConfig;
import me.bcloud.web.domain.WorkUser;
import me.bcloud.web.spring.Cache;
import org.springframework.stereotype.Controller;

@Controller("result")
public class ResultServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Resource
	private Cache cache;
	
	public void setCache(Cache cache) {
		this.cache = cache;
	}
	
	@Resource
	private UserDao userDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		int id = 0;
		try {
			id = Integer.parseInt(req.getParameter("id"));
		} catch (Exception e) {
			return;
		}
		Integer jobId = (Integer)req.getSession().getAttribute("job");
		
		// 获取结果 三种情况 0错误 1正确 2异常
		JobConfig jobConfig = cache.getJobMap().get(jobId);
		HashMap<Integer, WorkUser> workUsers = cache.getWorkMap().get(jobId);
		String resultStr = req.getParameter("result");

		if (resultStr.equals("0")) {

			
			WorkUser workUser = workUsers.remove(id);

			if (null == workUser) {
				System.out.println("not found workuser");
			}
			System.out.println(id);
			resp.getWriter().print("ok");
			return;
		}

		if (resultStr.equals("1")) {
			WorkUser workUser = workUsers.remove(id);
			if (null == workUser) {
				System.out.println("not found workuser");
				resp.getWriter().print("ok");
				return;
			}

			
			String resultUser = req.getParameter("u");
			// 定义规则  表1,内容1,表2,内容2,表3,内容3
			if (resultUser==null || resultUser.equals("")) {
				userDao.saveUser(jobConfig.getWriteTable(),workUser.getUser());
				resp.getWriter().print("ok");
				return;
			}
	
			String[] linesStrings=resultUser.split(",");
			StringBuilder sb1=new StringBuilder();
			StringBuilder sb2=new StringBuilder();
			for (int i = 0; i < linesStrings.length; i++) {
				if ((i+1)%2==0) {
					try {
						Integer.parseInt(linesStrings[i]);
						sb1.append(linesStrings[i] + ",");
					} catch (Exception e) {
						sb1.append("'" + linesStrings[i] + "',");
					}
					
				}else {

					sb2.append(linesStrings[i] + ",");
					
				}
			}
			// 写入数据库
			

			String s1=sb1.substring(0, sb1.length()-1) + ")";
			String s2=sb2.substring(0, sb2.length()-1) + ")";
			userDao.saveUser(jobConfig.getWriteTable(), workUser.getUser(), s1, s2);
			resp.getWriter().print("ok");
			return;
			
		}

		//异常  以后处理
		if (resultStr.equals("2")) {

		}
		resp.getWriter().print("ok");


	}
	


//
//	public Cache getCache() {
//		return cache;
//	}

}
